package com.banmao.CarlCamp.Day08;

/**
 * https://leetcode.cn/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/
 */
public class Offer58 {

    public String reverseLeftWords(String s, int n) {

        if (n == 0 || s == null || s.length() == 0) {
            return s;
        }
        // 使用中间数组
        char[] tempChars = new char[n];

        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            // 将 n 位前的字符放入中间数组
            if (i < n) {
                tempChars[i] = chars[i];
                continue;
            }
            // 将 n 位及之后的字符前移 n 位
            chars[i - n] = chars[i];
        }

        for (int i = 0; i < n; i++) {
            // 将中间数组的字符放回原数组
            chars[chars.length - n + i] = tempChars[i];
        }

        return String.valueOf(chars);
    }

    public static void main(String[] args) {
        Offer58 offer58 = new Offer58();
        System.out.println(offer58.reverseLeftWords("lrloseumgh", 6));
    }

}
